// 运行结果：
// Product parts: PartA, PartB, PartC
// Displaying ConcreteProductA
// Displaying ConcreteProductB
public class Product {
    private String partA;
    private String partB;
    private String partC;

    public void display() {
        System.out.println("Product parts: " + partA + ", " + partB + ", " + partC);
    }

    public void setPartA(String partA) {
        this.partA = partA;
    }

    public void setPartB(String partB) {
        this.partB = partB;
    }

    public void setPartC(String partC) {
        this.partC = partC;
    }
}
public interface Builder {
    void buildPartA();
    void buildPartB();
    void buildPartC();
    Product getResult();
}
public class ConcreteBuilder implements Builder {
    private Product product = new Product();

    @Override
    public void buildPartA() {
        product.setPartA("PartA");
    }

    @Override
    public void buildPartB() {
        product.setPartB("PartB");
    }

    @Override
    public void buildPartC() {
        product.setPartC("PartC");
    }

    @Override
    public Product getResult() {
        return product;
    }
}
public class Director {
    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    public Product construct() {
        builder.buildPartA();
        builder.buildPartB();
        builder.buildPartC();
        return builder.getResult();
    }
}
public interface AbstractProduct {
    void display();
}
public class ConcreteProductA implements AbstractProduct {
    @Override
    public void display() {
        System.out.println("Displaying ConcreteProductA");
    }
}
public class ConcreteProductB implements AbstractProduct {
    @Override
    public void display() {
        System.out.println("Displaying ConcreteProductB");
    }
}
public interface AbstractFactory {
    AbstractProduct createProductA();
    AbstractProduct createProductB();
}
public class ConcreteFactory implements AbstractFactory {
    @Override
    public AbstractProduct createProductA() {
        return new ConcreteProductA();
    }

    @Override
    public AbstractProduct createProductB() {
        return new ConcreteProductB();
    }
}
public class Client {
    public static void main(String[] args) {
        // 使用建造者模式
        Builder builder = new ConcreteBuilder();
        Director director = new Director(builder);
        Product product = director.construct();
        product.display();

        // 使用抽象工厂模式
        AbstractFactory factory = new ConcreteFactory();
        AbstractProduct productA = factory.createProductA();
        AbstractProduct productB = factory.createProductB();
        productA.display();
        productB.display();
    }
}